/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	adolf 
 * 
 * <p>描   述  :   BksnAction.java for com.pengtu.web.asset.assets    </p>
 * 
 * <p>最后修改 : $: 2011-8-17-下午02:57:00 v 1.0.0	 adolf   $     </p>
 * 
 */

package com.pengtu.web.asset.book;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.asset.book.BkInformation;
import com.pengtu.entity.asset.book.BkSn;
import com.pengtu.service.asset.BookManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 
 * BksnAction
 * 
 * 2011-8-17 下午02:57:00
 * 
 * @version 1.0.0
 * 
 */
public class BksnAction extends BaseActionSupport<BkSn> {
	/**
	 * serialVersionUID:TODO
	 * 
	 * @since 1.0.0
	 */

	private static final long serialVersionUID = 1L;

	private BookManager bookManager;

	private String id;
	private BkSn bksn;
	private Page<BkSn> page = new Page<BkSn>(10);

	public Page<BkSn> getPage() {
		return page;
	}

	public void setPage(Page<BkSn> page) {
		this.page = page;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setBookManager(BookManager bookManager) {
		this.bookManager = bookManager;
	}

	@Override
	public BkSn getModel() {
		// TODO Auto-generated method stub
		return bksn;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryfilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		if (!page.isOrderBySetted()) {
			page.setOrderBy("bookinfo.bookName");
			page.setOrder(Page.DESC);
		}
		page = bookManager.findPageBksn(page, queryfilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		String bookId = Struts2Utils.getParameter("bkid");
		String bookSn = Struts2Utils.getParameter("bkSn");
		String bookinfoId = Struts2Utils.getParameter("bookinfoId");
		if(StringUtils.isNotEmpty(bookId)){
			bksn = bookManager.getBkSn("id", bookId);
			bksn.setBookSN(bookSn);
			if(!bookinfoId.equals(bksn.getBookinfo().getId())){
				if(StringUtils.isNotEmpty(bookinfoId)){
					BkInformation bkInfo = bookManager.findByBIproperty("id", bookinfoId);
					if(null!=bkInfo)
						bksn.setBookinfo(bkInfo);
				}
			}
		}else{
			bksn.setBookSN(bookSn);
			if(StringUtils.isNotEmpty(bookinfoId)){
				BkInformation bkInfo = bookManager.findByBIproperty("id", bookinfoId);
				if(null!=bkInfo)
					bksn.setBookinfo(bkInfo);
			}
		}
		bookManager.saveBkSn(bksn);
		Struts2Utils.renderJson("1");
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		String bkid = Struts2Utils.getParameter("bkid");
		bookManager.deleteBkSn(bkid);
		Struts2Utils.renderJson("1");
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isNotEmpty(id)){
			bksn = bookManager.getBkSn("id", id);
		}else{
			bksn = new BkSn();
		}
	}
}
